import React, {Component, PropTypes} from 'react';
import {connect} from 'react-redux';
import {withRouter} from 'react-router';

import Header from '../../../components/Header';
import Back from '../../../components/Back';

import {loadSportDetail} from '../actions/HgActionPart';
import {parseQuery} from '../../../utils/url';
import CreateOrderInfo from '../components/CreateOrderInfo';

import {RES_OK_CODE} from '../../../constants/AppConstant';
import LoadingComponent from '../../../components/LoadingComponent';

class CreateOrderContainer extends LoadingComponent {
  
  constructor(props) {
    super(props);
  }

  componentWillMount() {
    const {dispatch, location} = this.props;
    let query = parseQuery(location.search);
    dispatch(loadSportDetail(query));
  }

  componentWillReceiveProps(nextProps) {
    const {hgsport} = nextProps;
    const {history} = this.props;
    let sportDetail = hgsport.huangguan.get('sportDetail');
    let apiRes = hgsport.huangguan.get('apiRes');
    this.closeLoading();

    if (apiRes.errorCode != RES_OK_CODE) {
      alert(apiRes.msg);
      history.goBack();
    }
  }

  render() {
    const {userModule, hgsport} = this.props;
    let sportDetail = hgsport.huangguan.get('sportDetail');
    return (
      <div className="hg-page page create-order-page">
        <Header {...this.props} className="hgsport-header">
          <Back />
          <h3>确认注单</h3>
        </Header>
        <div className="page-body">
          <div className="user-info">
            <span>{'用户名 : ' +userModule.user.get('auth').get('userName')}</span>
            <span>{'余额 : ' + userModule.user.get('info').userMoney}</span>
          </div>
          <div className="order-info-board">
            <div className="wrap">
              <CreateOrderInfo {...this.props} detail={ sportDetail }/>
            </div>
          </div>
        </div>
      </div>
    );
  }
};

function mapStateToProps(state) {
  const {app, userModule, hgsport} = state;
  return {app, userModule, hgsport};
}

export default connect(mapStateToProps)(withRouter(CreateOrderContainer));